﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace UTable.Core.Configuration
{
    public static class ConfigurationXMLSerializer
    {
        #region Load

        /// <summary>
        /// Load UTable Configuration from an XML file 
        /// </summary>
        /// <param name="path">Path of the XML file</param>
        /// <returns>UTable Configuration loaded from the XML file</returns>
        public static Configuration Load(string path)
        {
            Configuration configuration = null;
            try
            {
                using (TextReader textReader = new StreamReader(path))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
                    configuration = xmlSerializer.Deserialize(textReader) as Configuration;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return configuration;
        }

        #endregion

        #region Save

        public static void Save(Configuration configuration, string path)
        {
            using (TextWriter textWriter = new StreamWriter(path))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
                xmlSerializer.Serialize(textWriter, configuration);
            }
        }

        #endregion
    }
}
